package com.yqg.protection.netty.service;

import com.yqg.protection.entity.EquipmentInputStore;
import com.yqg.protection.service.IEquipmentInputStoreService;
import com.yqg.protection.service.ITemporaryDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class Sdf300RegisterService extends ProtocolService {

    @Autowired
    private IEquipmentInputStoreService equipmentInputStoreService;

    @Autowired
    ITemporaryDataService temporaryDataService;

    public void saveRegistration(String imei, String imsi, String uid, int[] la,
                                 int addressMode, int idType, int upMode,
                                 int upTime, int upUnit, int reserved, String sendTime) {
        try {
            log.info("SDF300设备注册信息: IMEI={}, IMSI={}, UID={}", imei, imsi, uid);

            //更新SIM卡编号
            EquipmentInputStore equipmentInputStore = getEquipmentInputStore(imei);
            if(imsi!= null && !imsi.equals("") && (equipmentInputStore.getSimId() == null || equipmentInputStore.getSimId().equals(""))) {
                equipmentInputStore.setUpdateTime(sendTime);
                equipmentInputStore.setSimId(imsi);
                equipmentInputStoreService.saveOrUpdate(equipmentInputStore);
            }
            //记录设备数据上传内容
            saveFireSysData(imei, "上传SDF300设备注册信息", sendTime);
            //更新设备数据上传时间(用于记录设备是否在线)
            updateTemporaryData(imei, sendTime);
            log.info("SDF300设备注册信息保存成功: {}", imei);
        } catch (Exception e) {
            log.error("SDF300保存设备注册信息失败: {}", e.getMessage(), e);
        }
    }
}
